package com.ilovepostcard;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ilovepostcard.controller.ServerAsyncTask;
import com.ilovepostcard.controller.server.PostcardResult;
import com.ilovepostcard.controller.server.ServerFactory;

public class PrintChooseCityActivity extends BaseActivity{
    
    public static final String KEY_PROVINCE = "KEY_PROVINCE";
    public static final String KEY_CITY = "KEY_CITY";
	
	ListView cityList;
	ImageView mReturn;
	LinkedHashMap<String, ArrayList<String>> cityTable;
	ArrayList<String> provinceList;
	
	private String mSelectProvince="上海";
	private String mselectCity;
	ListAdapter adapter;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_print_choosecity);
		init();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
	        if (adapter.level == 1) {
                adapter.level = 0;
                adapter.list = provinceList;
                adapter.notifyDataSetChanged();
                return true;
            }
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	private void init()
	{
		cityList = (ListView)findViewById(R.id.city_list);
		mReturn = (ImageView)findViewById(R.id.title_return);
		mReturn.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v) {
			    if (adapter.level == 1) {
                    adapter.level = 0;
                    adapter.list = provinceList;
                    adapter.notifyDataSetChanged();
                    return;
			    }
				finish();
				
			}
			
		});
		new ServerAsyncTask(this) {
            
            @Override
            protected void onSuccess(PostcardResult result) {
                cityTable = result.getCityTable();
                provinceList = new ArrayList<String>();
                for (String province : cityTable.keySet()) {
                    provinceList.add(province);
                }
                adapter = new ListAdapter(0, provinceList);
                cityList.setAdapter(adapter);
                cityList.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if (adapter.level == 0) {
                            mSelectProvince = adapter.list.get(position);
                            ArrayList<String> cities = cityTable.get(mSelectProvince);
                            adapter.level = 1;
                            adapter.list = cities;
                            adapter.notifyDataSetChanged();
                        } else if (adapter.level == 1) {
//                            if (position == 0) {
//                                adapter.level = 0;
//                                adapter.list = provinceList;
//                                adapter.notifyDataSetChanged();
//                            } else {
                                mselectCity = adapter.list.get(position);
                                Intent data = new Intent();
                                data.putExtra(KEY_PROVINCE, mSelectProvince);
                                data.putExtra(KEY_CITY, mselectCity);
                                setResult(Activity.RESULT_OK, data);
                                finish();
//                            }
                        }
                    }
                    
                });
            }
            
            @Override
            protected PostcardResult doRequest() {
                return ServerFactory.getServer().listCity();
            }
        }.execute();
		
	}

	
	class ListAdapter extends BaseAdapter
	{

	    int level = 0;/* 0: province, 1: city*/
		ArrayList<String> list;
		
		
		
		public ListAdapter(int level, ArrayList<String> list) {
            super();
            this.level = level;
            this.list = list;
        }

        @Override
		public int getCount() {
//            if (level == 1)
//                return list.size() + 1;
			return list.size();
		}

		@Override
		public Object getItem(int position) {
//		    if (level == 1) {
//		        if (position == 0)
//		            return null;
//		        return list.get(position - 1);
//		    }
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			if(convertView == null)
			{
				convertView = new TextView(PrintChooseCityActivity.this);
				AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,50);
				((TextView)convertView).setLayoutParams(params);
				((TextView)convertView).setPadding(30, 0, 30, 0);
				((TextView)convertView).setTextColor(Color.BLACK);
				
				
			}
//			if (level == 1) {
//			    if (position == 0)
//			        ((TextView)convertView).setText("上一级");
//			    else
//			        ((TextView)convertView).setText(list.get(position - 1));
//			} else {
			    ((TextView)convertView).setText(list.get(position));
//			}
			return convertView;
		}
		
	}
}
